//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "MTBrownianGeneratorFactory.h"
using namespace Cephei::QL::Models::Marketmodels::Browniangenerators;
#include <gen/QL/Models/Marketmodels/BrownianGenerator.h>
#include <gen/QL/Models/Marketmodels/BrownianGeneratorFactory.h>
using namespace Cephei::QL::Models::Marketmodels;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::Browniangenerators::CMTBrownianGeneratorFactory::CMTBrownianGeneratorFactory (boost::shared_ptr<QuantLib::MTBrownianGeneratorFactory>& childNative, Object^ owner) : CBrownianGeneratorFactory(CMTBrownianGeneratorFactory::typeid)
{
#ifdef HANDLE
	_phMTBrownianGeneratorFactory = NULL;
#endif
	_ppMTBrownianGeneratorFactory = &childNative;
    _ppBrownianGeneratorFactory = new boost::shared_ptr<QuantLib::BrownianGeneratorFactory> (boost::dynamic_pointer_cast<QuantLib::BrownianGeneratorFactory> (*_ppMTBrownianGeneratorFactory));
}
Cephei::QL::Models::Marketmodels::Browniangenerators::CMTBrownianGeneratorFactory::CMTBrownianGeneratorFactory (QuantLib::MTBrownianGeneratorFactory& childNative, Object^ owner) : CBrownianGeneratorFactory(CMTBrownianGeneratorFactory::typeid)
{
#ifdef HANDLE
	_phMTBrownianGeneratorFactory = NULL;
#endif
	_ppMTBrownianGeneratorFactory = new boost::shared_ptr<QuantLib::MTBrownianGeneratorFactory> (&childNative);
    _ppBrownianGeneratorFactory = new boost::shared_ptr<QuantLib::BrownianGeneratorFactory> (boost::dynamic_pointer_cast<QuantLib::BrownianGeneratorFactory> (*_ppMTBrownianGeneratorFactory));
    _MTBrownianGeneratorFactoryOwner = owner;
    _BrownianGeneratorFactoryOwner = owner;
}

Cephei::QL::Models::Marketmodels::Browniangenerators::CMTBrownianGeneratorFactory::CMTBrownianGeneratorFactory (CMTBrownianGeneratorFactory^ copy) : CBrownianGeneratorFactory(CMTBrownianGeneratorFactory::typeid)
{
#ifdef HANDLE
	_phMTBrownianGeneratorFactory = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppMTBrownianGeneratorFactory = new boost::shared_ptr<QuantLib::MTBrownianGeneratorFactory> (copy->GetShared());
        _ppBrownianGeneratorFactory = new boost::shared_ptr<QuantLib::BrownianGeneratorFactory> (boost::dynamic_pointer_cast<QuantLib::BrownianGeneratorFactory> (*_ppMTBrownianGeneratorFactory));
    }
}
Cephei::QL::Models::Marketmodels::Browniangenerators::CMTBrownianGeneratorFactory::CMTBrownianGeneratorFactory (System::Type^ t) : CBrownianGeneratorFactory(CMTBrownianGeneratorFactory::typeid)
{
#ifdef HANDLE
	_phMTBrownianGeneratorFactory = NULL;
#endif
	if (!t->IsSubclassOf(CMTBrownianGeneratorFactory::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::Browniangenerators::CMTBrownianGeneratorFactory::CMTBrownianGeneratorFactory (QuantLib::Handle<QuantLib::MTBrownianGeneratorFactory>& childNative, Object^ owner)  : CBrownianGeneratorFactory(CMTBrownianGeneratorFactory::typeid)
{
	_phMTBrownianGeneratorFactory = &childNative;
	_ppMTBrownianGeneratorFactory = &static_cast<boost::shared_ptr<QuantLib::MTBrownianGeneratorFactory>>(childNative.currentLink());
    _ppBrownianGeneratorFactory = new boost::shared_ptr<QuantLib::BrownianGeneratorFactory> (boost::dynamic_pointer_cast<QuantLib::BrownianGeneratorFactory> (*_ppMTBrownianGeneratorFactory));
    _MTBrownianGeneratorFactoryOwner = owner;
}
Cephei::QL::Models::Marketmodels::Browniangenerators::CMTBrownianGeneratorFactory::CMTBrownianGeneratorFactory (QuantLib::Handle<QuantLib::MTBrownianGeneratorFactory> childNative)  : CBrownianGeneratorFactory(CMTBrownianGeneratorFactory::typeid)
{
	_phMTBrownianGeneratorFactory = &childNative;
	_ppMTBrownianGeneratorFactory = &static_cast<boost::shared_ptr<QuantLib::MTBrownianGeneratorFactory>>(childNative.currentLink());
    _ppBrownianGeneratorFactory = new boost::shared_ptr<QuantLib::BrownianGeneratorFactory> (boost::dynamic_pointer_cast<QuantLib::BrownianGeneratorFactory> (*_ppMTBrownianGeneratorFactory));
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::Browniangenerators::CMTBrownianGeneratorFactory::CMTBrownianGeneratorFactory (QuantLib::MTBrownianGeneratorFactory childNative)  : CBrownianGeneratorFactory(CMTBrownianGeneratorFactory::typeid)
{
#ifdef HANDLE
	_phMTBrownianGeneratorFactory = NULL;
#endif
	_ppMTBrownianGeneratorFactory = new boost::shared_ptr<QuantLib::MTBrownianGeneratorFactory> (new QuantLib::MTBrownianGeneratorFactory (childNative));
    _ppBrownianGeneratorFactory = new boost::shared_ptr<QuantLib::BrownianGeneratorFactory> (boost::dynamic_pointer_cast<QuantLib::BrownianGeneratorFactory> (*_ppMTBrownianGeneratorFactory));
}
#endif

Cephei::QL::Models::Marketmodels::Browniangenerators::CMTBrownianGeneratorFactory::~CMTBrownianGeneratorFactory ()
{
    if (_ppMTBrownianGeneratorFactory != NULL)
    {
	    delete _ppMTBrownianGeneratorFactory;
        _ppMTBrownianGeneratorFactory = NULL;
    }
}
Cephei::QL::Models::Marketmodels::Browniangenerators::CMTBrownianGeneratorFactory::!CMTBrownianGeneratorFactory ()
{
    if (_ppMTBrownianGeneratorFactory != NULL)
    {
	    delete _ppMTBrownianGeneratorFactory;
    }
}
QuantLib::MTBrownianGeneratorFactory& Cephei::QL::Models::Marketmodels::Browniangenerators::CMTBrownianGeneratorFactory::GetReference ()
{
    if (_ppMTBrownianGeneratorFactory == NULL) throw gcnew NativeNullException ();
	return **_ppMTBrownianGeneratorFactory;
}
boost::shared_ptr<QuantLib::MTBrownianGeneratorFactory>& Cephei::QL::Models::Marketmodels::Browniangenerators::CMTBrownianGeneratorFactory::GetShared ()
{
    if (_ppMTBrownianGeneratorFactory == NULL) throw gcnew NativeNullException ();
	return *_ppMTBrownianGeneratorFactory;
}
QuantLib::MTBrownianGeneratorFactory* Cephei::QL::Models::Marketmodels::Browniangenerators::CMTBrownianGeneratorFactory::GetPointer ()
{
    if (_ppMTBrownianGeneratorFactory == NULL) throw gcnew NativeNullException ();
	return &**_ppMTBrownianGeneratorFactory;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::MTBrownianGeneratorFactory>& Cephei::QL::Models::Marketmodels::Browniangenerators::CMTBrownianGeneratorFactory::GetHandle ()
{
	if (_phMTBrownianGeneratorFactory == NULL)
	{
		_phMTBrownianGeneratorFactory = new Handle<QuantLib::MTBrownianGeneratorFactory> (*_ppMTBrownianGeneratorFactory);
	}
	return *_phMTBrownianGeneratorFactory;
}
#endif
bool Cephei::QL::Models::Marketmodels::Browniangenerators::CMTBrownianGeneratorFactory::HasNative () 
{
	return (_ppMTBrownianGeneratorFactory != NULL);
}

Cephei::QL::Models::Marketmodels::IBrownianGenerator^ Cephei::QL::Models::Marketmodels::Browniangenerators::CMTBrownianGeneratorFactory::Create (UInt64 factors, UInt64 steps)
{
    try
    {
        QuantLib::Size _factors = (QuantLib::Size)ValueHelper::Convert (factors);
        QuantLib::Size _steps = (QuantLib::Size)ValueHelper::Convert (steps);
    	boost::shared_ptr<QuantLib::BrownianGenerator> _rv = (boost::shared_ptr<QuantLib::BrownianGenerator>)(*_ppMTBrownianGeneratorFactory)->create ( _factors,  _steps );   
        Cephei::QL::Models::Marketmodels::CBrownianGenerator^ _nrv = gcnew Cephei::QL::Models::Marketmodels::CBrownianGenerator (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

